import { resolve } from 'core-js/fn/promise';
import { replace } from 'lodash';
import Vue from 'vue';
import VueRouter from 'vue-router';
import RouterInit from './router.init.js';
Vue.use(VueRouter);
// 编程式导航方法重新定义
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};
// 基础路由跳转定义
const routes = [
    { path: '/', redirect: { path: '/app/dashboard'}},
    { path: '/app', component: () => import('@/compoments/common/Home'), meta: { title: "功能模块"}},
    { path: '/api', component: () => import('@/compoments/common/Home'), meta: { title: "api说明"}},
    { path: '/login', component: () => import('@/compoments/common/Login') }
];
const createRouter = new VueRouter({
    mode: "history",
    routes: routes
});
// 请求前判断
const _rt = new RouterInit(createRouter);
createRouter.beforeEach((to, from , next)=>{
    if(to.path === '/login'){
        next();
    } else {
        if (navigator.userAgent.indexOf('MSIE') > -1 && to.path === '/editor') {
            // 简单的判断IE10及以下不进入富文本编辑器，该组件不兼容
            Vue.prototype.$alert('vue-quill-editor组件不兼容IE9及以下浏览器，请使用更高版本的浏览器查看', '浏览器不兼容通知', {
                confirmButtonText: '确定'
            });
        } else {
            if(toPath(to, "/app") || toPath(to, "/api")){
                // 若请求地址为app 或者 app 子菜单
                if(to.path === "/app") next({path: "/app/dashboard"});
                if(!_rt.hasRouters( createRouter )){
                    _rt.initRoutes( createRouter, to, next);
                } else {
                    next({to: to, replace: true});
                }
            } else {
                next();
            }
        }
    }
})
function toPath(to,path){
    return to.path === path || to.path.indexOf(path) != -1;
}
export default createRouter;
